<?php
	include_once './init.php';
	include_once './login.php';
	include_once './session.php';

	$method = $_POST["method"];
	switch ($method) {
		case 'recommand':
			getRec();
			break;
		case 'mine':
			addVideoData();
			break;
		case 'current':
			getCurrentData();
			break;
		case 'collect':
			collect();
			break;
		case 'score':
			score();
			break;
		case 'add':
			addPlayTimes();
			break;
		case 'collectionList':
			getCollectionList();
			break;
		case 'uploadList':
			getUploadList();
			break;
	}
	function getRec(){
		$db = initDatabase();
		$recList = $db->get_results("select * from video where videoStatus=0 order by playSum desc limit 0 , 20");
		
		showmsg($recList);
	}
	function getCurrentData(){
		$videoId = $_POST["videoId"];
		$db = initDatabase();
		$currList = $db -> get_row("select * from video where videoId='$videoId'");
		
		$currList->isCollected = isCollected();
		$currList->isScored = isScored();

		showmsg($currList);
	}
	function addPlayTimes(){
		$videoId = $_POST["videoId"];
		$db = initDatabase();
		$db -> query("update video set playSum=(playSum+1) where videoId='$videoId'");
	}
	function collect(){
		if(isLogin()){
			if(isCollected()){
				$videoId = $_POST["videoId"];
				$userId = $_SESSION["userId"];
				$db = initDatabase();
				$resultOfInsert = $db ->query("delete from collect where userId='$userId' and videoId='$videoId'");
				
				if($resultOfInsert){
					showmsg($collect = array(
						'status' => 'success',
						'message' => 'collect successfully'
						));								
				}else{
					showmsg($collect = array(
						'status' => 'fail',
						'message' => 'collect failed'
						));				
				}
			}else{
				$videoId = $_POST["videoId"];
				$userId = $_SESSION["userId"];
				$db = initDatabase();
				$resultOfInsert = $db ->query("insert into collect (userId , videoId) values('$userId' , '$videoId')");
				
				if($resultOfInsert){
					showmsg($collect = array(
						'status' => 'success',
						'message' => 'collect successfully'
						));								
				}else{
					showmsg($collect = array(
						'status' => 'fail',
						'message' => 'collect failed'
						));				
				}
			}
		}else{
			showmsg($login = array(
				'status' => 'fail',
				'message' => 'please login'
				));
		}
	}
	function score(){
		if(isLogin()){
			if(isScored()){
				showmsg($score = array(
				'status' => 'fail',
				'message' => 'you have scored'
				));
			}else{
				$userId = $_SESSION["userId"];
				$videoId = $_POST["videoId"];
				$score = $_POST["score"];

				$db = initDatabase();
				//更新score表
				$resultOfScoreInsert = $db ->query("insert into score (videoId , userId , score) values ('$videoId' , '$userId' , '$score')");
				showmsg($score = array(
				'status' => 'success',
				'message' => 'score successfully'
				));

			}
		}else{
			showmsg($login = array(
			'status' => 'fail',
			'message' => 'please login'
			));
		}
	}
	function isCollected(){
		if(isLogin()){
			$videoId = $_POST["videoId"];
			$userId = $_SESSION["userId"];
			$db = initDatabase();
			$resultOfCollect = $db -> get_row("select * from collect where userId='$userId' and videoId='$videoId'");
			if($resultOfCollect){
				return true;
			}else{
				return false;
			}
		}else{
			return false;			
		}
	}
	function isScored(){
		$videoId = $_POST["videoId"];
		$userId = $_SESSION["userId"];

		$db = initDatabase();
		$resultOfScore = $db -> get_row("select * from score where userId='$userId' and videoId='$videoId'");
		if($resultOfScore){
			return true;
		}else{
			return false;
		}
	}
	function getCollectionList(){
		if(isLogin()){
			$userId = $_SESSION["userId"];
			$db = initDatabase();
			$resultOfCollection = $db ->get_results("select * from video where videoId in (select videoId from collect where userId='$userId')");
			if($resultOfCollection){
				showmsg($resultOfCollection);
			}else{
				showmsg('fail');
			}
		}else{
			showmsg($login = array(
				'status' => 'fail',
				'message' => 'please login'
				));
		}
	}
	function getUploadList(){
		if(isLogin()){
			$userId = $_SESSION["userId"];
			
			$db = initDatabase();
			$resultOfUpload = $db -> get_results("select * from video where uploaderId='$userId'");
			if($resultOfUpload){
				showmsg($resultOfUpload);			
			}else{
				showmsg('fail');
			}
		}else{
			showmsg($login = array(
				'status' => 'fail',
				'message' => 'please login'
				));
		}
	}
?>